#include "FilePicker.h"
#include <QFileDialog>

FilePicker::FilePicker(QObject *parent)
    : QObject{parent}
{}

QList<FileModel> FilePicker::pickFiles() {
    QStringList fileList = QFileDialog::getOpenFileNames(nullptr, tr("打开文件"), "", tr("所有文件(*)"));
    QList<FileModel> files = QList<FileModel>();
    if (!fileList.isEmpty()) {
        for (QString filePath : fileList) {
            QFileInfo fileInfo(filePath);
            FileModel model = FileModel();
            model.size = fileInfo.size();
            model.path = fileInfo.filePath();
            model.format = fileInfo.suffix();
            model.name = fileInfo.baseName();
            model.createDate = fileInfo.birthTime();
            model.editDate = fileInfo.lastModified();
            model.fold = fileInfo.absolutePath();
            files.append(model);
        }
    }
    return files;
}

FileModel FilePicker::pickFile() {
    QString filePath = QFileDialog::getOpenFileName(nullptr, tr("打开文件"), "", tr("所有文件(*)"));
    FileModel model = FileModel();
    if (!filePath.isEmpty()) {
        QFileInfo fileInfo(filePath);
        model.size = fileInfo.size();
        model.path = fileInfo.filePath();
        model.format = fileInfo.suffix();
        model.name = fileInfo.baseName();
        model.createDate = fileInfo.birthTime();
        model.editDate = fileInfo.lastModified();
        model.fold = fileInfo.absolutePath();
    }
    return model;
}

QList<FileModel> FilePicker::pickDirType(LocationDirType type) {
    return FilePicker::pickDirPath(FilePicker::locationPath(type));
}

QList<FileModel> FilePicker::pickDirPath(QString dirPath) {
    QString dirFold = QFileDialog::getExistingDirectory(nullptr, tr("打开文件"), dirPath);
    QDir dir(dirFold);
    QStringList fileList = dir.entryList(QDir::Files);
    QList<FileModel> files = QList<FileModel>();
    if (!fileList.isEmpty()) {
        for (QString filePath : fileList) {
            QFileInfo fileInfo(filePath);
            if (fileInfo.isDir()) {
                // for (FileModel file : FilePicker::pickDirPath(filePath)) {
                //     fileList.append(file);
                // }
            }else {
                FileModel model = FileModel();
                model.size = fileInfo.size();
                QString fold = dirFold;
                model.format = fileInfo.suffix();
                model.name = fileInfo.baseName();
                model.createDate = fileInfo.birthTime();
                model.editDate = fileInfo.lastModified();
                model.fold = fold;
                model.path = fold.append("/").append(fileInfo.filePath());
                files.append(model);
            }
        }
    }
    return files;
}

QString FilePicker::locationPath(LocationDirType type) {
    switch (type) {
    case DesktopDir:
        return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    case DocumentsDir:
        return QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
    case FontsDirn:
        return QStandardPaths::writableLocation(QStandardPaths::FontsLocation);
    case ApplicationsDir:
        return QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
    case MusicDir:
        return QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
    case MoviesDir:
        return QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
    case PicturesDir:
        return QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    case TempDir:
        return QStandardPaths::writableLocation(QStandardPaths::TempLocation);
    case HomeDir:
        return QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    case AppLocalDataDir:
        return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
    case CacheDir:
        return QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
    case GenericDataDir:
        return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
    case RuntimeDir:
        return QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
    case ConfigDir:
        return QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
    case DownloadDir:
        return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
    case GenericCacheDir:
        return QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
    case GenericConfigDir:
        return QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
    case AppDataDir:
        return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    case AppConfigDir:
        return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    case PublicShareDir:
        return QStandardPaths::writableLocation(QStandardPaths::PublicShareLocation);
    case TemplatesDir:
        return QStandardPaths::writableLocation(QStandardPaths::TemplatesLocation);
    case StateDir:
        return QStandardPaths::writableLocation(QStandardPaths::StateLocation);
    case GenericStateDir:
        return QStandardPaths::writableLocation(QStandardPaths::GenericStateLocation);
    default:
        break;
    }
    return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
}
